#include			"ScreenManager.hpp"
#include			"ImgManager.hpp"
#include			"Enums.hpp"

#include			<iostream>


ScreenManager::ScreenManager(sf::RenderWindow* scr)
{
	_screen = scr;
	_back = new sf::Sprite(ImgManager::getImage("back"));
}

ScreenManager::~ScreenManager()
{
	if (_back != 0)
	delete (_back);
}

void			ScreenManager::proceed()
{
	this->_screen->Clear();
	this->displayBackground();
	this->displayPowerUps();
	this->displayPlayers();
	this->displayMonsters();
	this->displayObstacles();
	this->displayHUD();
	this->displayMessages();

	//this->_screen->Display();
}

GameObject*			ScreenManager::getNextTypedObject(GameObjectType type, int nbIt) const
{
	std::map<int, GameObject*>::iterator	itb = Game::_mapObj->begin();
	std::map<int, GameObject*>::iterator	ite = Game::_mapObj->end();

	for (; ite != itb; ++itb)
	{
		if (itb->second->getType() == type)
		{
			if (nbIt <= 0)
				//return (itb->second);
				return (Game::getObject(itb->second->getId()));
			nbIt --;
		}
	}
	return (0);
}

void			ScreenManager::displayBackground() const
{
	/*ATTENTION, TROUVER LE MOYEN (/PASSER LE ElapsedTime) DE BOUGER LE FOND D'ECRAN DE MANIERE LIEE AU TEMPS !!!*/
	
	this->_back->Move(-.25, 0);
	if (-this->_back->GetPosition().x > this->_back->GetSize().x / 2)
		this->_back->Move(-this->_back->GetPosition().x, 0);
	this->_screen->Draw(*(this->_back));
}

void			ScreenManager::displayPowerUps() const
{
	GameObject* obj = 0;
	int			i = -1;
	
	while ((obj = this->getNextTypedObject(PowerUp, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}

void			ScreenManager::displayPlayers() const
{
	GameObject* obj = 0;
	int			i = -1;

	while ((obj = this->getNextTypedObject(Player, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
	while ((obj = this->getNextTypedObject(Force, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}

void			ScreenManager::displayMonsters() const
{
	GameObject* obj = 0;
	int			i = -1;
	
	while ((obj = this->getNextTypedObject(Monster, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
	while ((obj = this->getNextTypedObject(Fire, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}

void			ScreenManager::displayObstacles() const
{
	GameObject* obj = 0;
	int			i = -1;
	
	while ((obj = this->getNextTypedObject(Obstacle, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}

void			ScreenManager::displayHUD() const
{
	GameObject* obj = 0;
	int			i = -1;
	
	while ((obj = this->getNextTypedObject(HUD, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}

void			ScreenManager::displayMessages() const
{
	GameObject* obj = 0;
	int			i = -1;
	
	while ((obj = this->getNextTypedObject(Message, ++i)) != 0)
		this->_screen->Draw(obj->getRepresentation());
}